package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendanceDedAddDTO;
import org.jsola.hr.dto.AttendanceDedUpdateDTO;
import org.jsola.hr.dto.AttendanceMonthlyAddDTO;
import org.jsola.hr.query.AttendanceDedQuery;
import org.jsola.hr.service.IAttendanceDedService;
import org.jsola.hr.vo.AttendanceDedListVO;
import org.jsola.hr.vo.AttendanceDedVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrAttendanceDedController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendanceDeds")
public class AttendanceDedController implements IAttendanceDedControllerAPI {

    @Autowired
    private IAttendanceDedService attendanceDedService;

    @Override
    @PostMapping
    public Result<AttendanceDedVO> save(@Valid @RequestBody AttendanceDedAddDTO attendanceDedAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增考勤扣款设置权限校验

        return Result.success(
                attendanceDedService.save(attendanceDedAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendanceDedUpdateDTO attendanceDedUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改考勤扣款设置权限校验

        int count = attendanceDedService.update(attendanceDedUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{attendanceDedId}")
    public Result<Integer> delete(@PathVariable String attendanceDedId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除考勤扣款设置权限校验

        int count = attendanceDedService.deleteByIds(tokenUser, attendanceDedId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendanceDedIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除考勤扣款设置权限校验

        int count = attendanceDedService.deleteByIds(tokenUser, attendanceDedIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendanceDedId}")
    public Result<AttendanceDedVO> findById(@PathVariable String attendanceDedId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看考勤扣款设置权限校验

        return Result.success(
            attendanceDedService.selectById(attendanceDedId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendanceDedListVO>> listAll(@Valid AttendanceDedQuery attendanceDedQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看考勤扣款设置权限校验

        return Result.success(
            attendanceDedService.select(attendanceDedQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendanceDedListVO>> page(@Valid AttendanceDedQuery attendanceDedQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            attendanceDedService.selectPage(attendanceDedQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping(value = "attendanceDedCount")
    public Result<Boolean> attendanceDedCount(@Valid @RequestBody AttendanceMonthlyAddDTO attendanceMonthlyAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增考勤扣款设置权限校验

        return Result.success(
                attendanceDedService.attendanceDedCount(tokenUser, attendanceMonthlyAddDTO)
        );
    }


}

